<%@ page import="java.util.jar.Attributes.Name"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="support.*,bean.*, java.util.*" %>
    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Degree-university</title>
<link href="main.css" rel="stylesheet" type="text/css">
</head>
<body>

<%
	// Get location from HTTP request
	Vector<Degree> degrees = (Vector<Degree>)session.getAttribute("degrees");
	Degree degree = null;
	if(degrees == null)
	{
		degrees = new Vector<Degree>();
		session.setAttribute("degrees", degrees);
	}
	degree = new Degree();
	degrees.add(degree);
	
	support s = new support();
	Vector universities = (Vector)application.getAttribute("universities");
	if(universities == null)
	{
		String path = config.getServletContext().getRealPath("universities.txt");
		universities = s.getUniversities(path);
		application.setAttribute("universities",universities);
	}
	
	String location_id = (String)request.getParameter("location_id");
	String location = (String)request.getParameter("location");
	int lid;
	if (location_id != null && location == null){  // User choose a exist location
		lid = Integer.valueOf(location_id);
	}
	else {										   // User manually enter a location
		Vector newLocation = new Vector();
		newLocation.add(location);
		newLocation.add(new Vector());
		universities.add(newLocation);
		lid = universities.size() - 1;
	}
	degree.setLocation(lid);
%>
<h1>Provide degrees - Choose University</h1>
	
	<table class="multicolumn">
		<tr>
			<% 
			Vector univState = (Vector) ((Vector)universities.get(lid)).get(1);
			
			int cols = 3;
			int itemsPerCol = univState.size() / cols;
			if (univState.size() % cols != 0) itemsPerCol += 1;
			for(int i=0; i<univState.size(); i++){
				if (i % itemsPerCol == 0)  out.println("<td style=\"vertical-align:top\">");
				out.println("<a href=\"discipline.jsp?univ_id=" + i + "\">" + univState.get(i) + "</a><br />");
				if (i % itemsPerCol == itemsPerCol-1 || i==univState.size() -1 )  out.println("</td>");
			};
			%>
		</tr>
	</table>
	<form name="form1" action="discipline.jsp" method="get">
	<h1>If not found, input your university below:</h1><br>
	<input type="text" name="university" value="">
	<input type="submit" value = "submit">
	</form>	
</body>
</html>